package main

import (
	"bufio"
	"fmt"
	"laxkits/lib/laxexec"
	"laxkits/lib/laxoffice"
	"laxkits/lib/laxutils"
	"os"
	"path"
	"strings"
)

const (
	header = `
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>%s</title>
</head>
<body>
<table border="1" cellspacing="0">
`
	footer = `
</body>
<style type="text/css">
	td {
		white-space: nowrap;
	}
</style>
</html>
`
)

func main() {
	r := bufio.NewReader(os.Stdin)
	for {
		fmt.Println("把文件拖到这里来按回车即可:")
		fn, err := r.ReadString('\n')
		if err != nil {
			break
		}
		fn = laxutils.ToLinuxPath(fn)
		if len(fn) < 1 {
			continue
		}
		if !laxutils.IsFileExists(fn) {
			fmt.Printf("输入非法,找不到文件:[%s]\n", fn)
			continue
		}
		realFileName := path.Base(fn)
		suffix := path.Ext(realFileName)
		prefix := realFileName[:len(realFileName)-len(suffix)]
		writeFn := path.Join(fn[:len(fn)-len(realFileName)], prefix+"_show.html")
		//fmt.Printf("输入内容: 文件名[%s], 后缀[%s],前缀[%s],生成到[%s]\n", realFileName, suffix,prefix,writeFn)
		w, err := laxoffice.NewLaxNormalFileWriter(writeFn)
		if err != nil {
			fmt.Println("打开文件准备写入数据，但是打开失败")
			continue
		}
		w.WriteString(fmt.Sprintf(header, prefix + "_show"))
		lastTlog := ""
		localWriteTlogTitle := func(title, tips []string) {
			w.WriteString("<tr>")
			for idx, t := range title {
				w.WriteString(fmt.Sprintf("<th title=\"%s\">%s</th>", tips[idx], t))
			}
			w.WriteString("</tr>\n")
		}
		localWriteLine := func (row []string) {
			w.WriteString("<tr>")
			for _, t := range row {
				w.WriteString(fmt.Sprintf("<td>%s</td>", t))
			}
			w.WriteString("</tr>\n")
		}
		err = laxoffice.FileEachRow(fn, 0, func(fileIndex int, fileRow string) error {
			if len(strings.TrimSpace(fileRow)) < 1 {
				return nil
			}
			fields := strings.Split(fileRow, "|")
			if lastTlog == fields[0] {
				// 等于上一个，直接写
				localWriteLine(fields)
				return nil
			}
			if lastTlog != "" {
				w.WriteString("</table>\n")
				w.WriteString("<br>\n")
				w.WriteString("<table border=\"1\" cellspacing=\"0\">\n")
			}
			lastTlog = fields[0]
			csvFinalFn := path.Join("./csv", fields[0]+".csv")
			//csvFinalFn := path.Join("E:\\RO_FOR_CLIENT\\config\\Table\\Tools\\Tlog\\csv", fields[0]+".csv")
			if !laxutils.IsFileExists(csvFinalFn) {
				fmt.Printf("第[%d]行解析失败,找不到csv文件[%s]\n", fileIndex, csvFinalFn)
				w.WriteString(fileRow)
			} else {
				title := []string{""}
				csvFields := []string{""}
				laxoffice.CSVEachRow(csvFinalFn, 3, func(csvIndex int, csvRow []string) error {
					if len(csvRow) > 0 {
						csvFields = append(csvFields, csvRow[0])
					} else {
						csvFields = append(csvFields, "")
					}
					if len(csvRow) > 2 {
						title = append(title, csvRow[2])
					} else {
						title = append(title, "")
					}
					return nil
				})
				localWriteTlogTitle(csvFields, title)
			}
			localWriteLine(fields)
			return nil
		})
		w.WriteString("</table>")
		w.WriteString(footer)
		w.Finish()
		if err == nil {
			fmt.Printf("解析成功,生成文件:[%s]\n\n", writeFn)
			if lc := laxexec.NewLaxCommand("start " + writeFn); lc != nil {
				lc.Start()
			}
		}
	}
}
